package com.eqdd.yiqidian.ui.base;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.BaseEnterActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.javonlee.dragpointview.OnPointDragListener;
import com.javonlee.dragpointview.view.AbsDragPointView;
import com.javonlee.dragpointview.view.DragPointView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-7.
 */

public abstract class BaseEnterActivity extends BaseActivity {

    public BaseEnterActivityCustom dataBinding;
    public List<EnterBean> enterBeans = new ArrayList<>();
    private SlimAdapterEx slimAdapterEx;


    public abstract void itemClick(int p);

    public abstract void initEnterData();

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_enter);

    }

    @Override
    public void initData() {
        initEnterData();

    }

    protected void updateNum(int pos, int num) {
        if (slimAdapterEx.getDataItem(pos) instanceof EnterBean) {
            EnterBean dataItem = (EnterBean) slimAdapterEx.getDataItem(pos);
            dataItem.setNum(num);
            slimAdapterEx.notifyItemChanged(pos);
        }
    }

    protected void updateNum(int pos) {
        if (slimAdapterEx.getDataItem(pos) instanceof EnterBean) {
            EnterBean dataItem = (EnterBean) slimAdapterEx.getDataItem(pos);
            if (dataItem.getCodes() != null) {
                int num = 0;
                for (int code : dataItem.getCodes()) {
                    num += GreenDaoUtil.getNum(code);
                }
                dataItem.setNum(num);
                slimAdapterEx.notifyItemChanged(pos);
            } else if (dataItem.getCode() != 0) {
                dataItem.setNum(GreenDaoUtil.getNum(dataItem.getCode()));
                slimAdapterEx.notifyItemChanged(pos);
            }
        }
    }

    @Override
    public void setView() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.setNestedScrollingEnabled(false);
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_15, new SlimInjector<EnterBean>() {
            @Override
            public void onInject(EnterBean data, IViewInjector injector) {
                injector.text(R.id.tv_title, data.getContent())
                        .gone(R.id.iv_head);
                if (data.getNum() > 0) {
                    injector.visible(R.id.drag_point_view);
                    injector.text(R.id.drag_point_view, data.getNum() + "");
                } else {
                    injector.gone(R.id.drag_point_view);

                }
            }
        }).register(R.layout.list_item_32, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {

            }
        }).attachTo(dataBinding.recyclerView).updateData(enterBeans);

        ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener(
                (rv, p, v) -> itemClick(p)
        );
        setDownView();
    }


    public void setDownView() {

    }
}